﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BDM
{
    public partial class frmPrijsUpdate : Form
    {
        #region Variables
        bool bFout = false;
        BDMForm formBDM;
        #endregion
        
        public frmPrijsUpdate(BDMForm frmBDM)
        {
            InitializeComponent();
            formBDM = frmBDM;
            poortenInladen();
        }

        private void poortenInladen()
        {
            Dictionary<int, string> dict = GetData.GetAllPorts();

            foreach (KeyValuePair<int, string> kvp in dict)
            {
                cbVan.Items.Add(kvp.Key.ToString() + " - " + kvp.Value.ToString());
            }
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cbVan_SelectedIndexChanged(object sender, EventArgs e)
        {
            cbTot.Enabled = true;
            cbTot.SelectedIndex = -1;
            cbSoort.Enabled = false;
            cbSoort.SelectedIndex = -1;
            tbCt.Text = "";
            tbCt.ReadOnly = true;

            Dictionary<int, string> dict = GetData.GetPortsPerPort((cbVan.SelectedIndex + 1).ToString());

            cbTot.Items.Clear();

            foreach (KeyValuePair<int, string> kvp in dict)
            {
                cbTot.Items.Add(kvp.Key.ToString() + " - " + kvp.Value.ToString());
            }
        }

        private void cbTot_SelectedIndexChanged(object sender, EventArgs e)
        {
            cbSoort.Enabled = true;
            cbSoort.SelectedIndex = -1;
            tbCt.Text = "";
            tbCt.ReadOnly = true;
        }

        private void btnPrijsUpdate_Click(object sender, EventArgs e)
        {
            if (bFout == true)
            {
                bFout = false;
                return;
            }
            
            bool bSpits = false;

            if (cbSoort.SelectedItem.ToString() == "Spitsprijs")
            {
                bSpits = true;
            }

            SetData.UpdatePrice(cbVan.SelectedItem.ToString()[0].ToString(), cbTot.SelectedItem.ToString()[0].ToString(), tbCt.Text.ToString(), bSpits);
        }

        private void cbSoort_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbTot.Enabled == true && cbTot.SelectedIndex != -1 && cbSoort.SelectedIndex != -1)
            {
                bool bSpits = false;
                if (cbSoort.SelectedItem.ToString() == "Spitsprijs")
                {
                    bSpits = true;
                }
                tbCt.Text = GetData.GetPricePerRoute((cbVan.SelectedIndex + 1).ToString(), (cbTot.SelectedItem.ToString()[0]).ToString(), bSpits);
                tbCt.ReadOnly = false;
            }
        }

        private void tbCt_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar.ToString() != "\b" && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void tbCt_Leave(object sender, EventArgs e)
        {
            if (tbCt.Text == "")
            {
                tbCt.Text = "0";
            }

            if (Convert.ToInt32(tbCt.Text) > 2631)
            {
                MessageBox.Show("De maximum prijs is overschreden!\r\nDe prijs is automatisch aangepast naar het hoogst mogelijke bedrag.");
                bFout = true;
                tbCt.Text = "2631";
            }
        }

        private void frmPrijsUpdate_FormClosing(object sender, FormClosingEventArgs e)
        {
            formBDM.bUpdateOpen = false;
        }
    }
}
